
package demo1;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;

public class Starfield {
    private Random rnd;
    private Star[] star;
    private int numStars;
    private double mWidth, mHeight, mDepth;
    
    public Starfield(int Width, int Height, int Depth, int Amount) {
        numStars = Amount;
        star = new Star[numStars];
        mWidth = Width;
        mHeight = Height;
        mDepth = Depth;
        rnd = new Random();
        for(int i=0; i<numStars; i++) {
            star[i] = new Star(rnd.nextInt(Width)-Width/2, rnd.nextInt(Height)-Height/2, rnd.nextInt(Depth));
        }
    }
    
    // Return the number of stars in the Starfield.
    public int numStars() {
        return numStars;
    }
    
    // Move a single star.
    public void moveStar(int starNum, double X, double Y, double Z) {
        if(starNum >= 0 && starNum < numStars)
            star[starNum].move(X, Y, Z);
    }
    
    // Move all stars in the Starfield.
    public void moveStarfield(double X, double Y, double Z) {
        for(int i=0; i<numStars; i++) {
            star[i].move(X, Y, Z);
            if(star[i].getZ() <= 0.0) star[i].move(0.0, 0.0, mDepth);
        }
    }
    
    // Draw the starfield to the screen.
    public void draw(Graphics g, int screenW, int screenH, double fovX, double fovY) {
        Arrays.sort(star);
        for(int i=0; i<numStars; i++)
            star[i].draw(g, screenW, screenH, fovX, fovY, mDepth);
    }
}
